/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.drug.entity.DrugBuyPlan;
import com.jims.master.modules.drug.entity.DrugSupplierCata;
import com.jims.master.modules.drug.service.DrugBuyPlanService;
import com.jims.master.modules.drug.service.DrugStockService;
import com.jims.master.modules.drug.service.DrugSupplierCataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 药品采购入库Controller
 * @author DT
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugBuyStorage")
public class DrugBuyStorageController extends BaseController {

    @Autowired
    private DrugBuyPlanService drugBuyPlanService;
    @Autowired
    private DrugStockService drugStockService;
    @Autowired
    private DrugSupplierCataService drugSupplierCataService;
    @ModelAttribute
    public DrugBuyPlan get(@RequestParam(required=false) String id) {
        DrugBuyPlan entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = drugBuyPlanService.get(id);
        }
        if (entity == null){
            entity = new DrugBuyPlan();
        }
        return entity;
    }

    /**
     * 药品采购功能
     * @param drugBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     * @date 2017-07-27
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(DrugBuyPlan drugBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (null != drugBuyPlan && null != drugBuyPlan.getPurchaseNumber()) {
            drugBuyPlan.setStateFlag("3");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        } else {
            drugBuyPlan.setDelFlag("4");
            Page<DrugBuyPlan> page = drugBuyPlanService.getDrugPlanList(drugBuyPlan);
            model.addAttribute("page", page);
        }
        //采购供应商
        List<DrugSupplierCata> list = drugSupplierCataService.findList(new DrugSupplierCata());
        model.addAttribute("cataList", list);
        return "modules/drug/drugBuyStorageIndex";
    }

    /**
     * 采购药品入库提供换厂商换药功能
     * @param drugBuyPlan
     * @param model
     * @return
     * @author CTQ
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "drugBuyForm")
    public String form(DrugBuyPlan drugBuyPlan, Model model) {
        model.addAttribute("drugBuyPlan", drugBuyPlan);
        return "modules/drug/drugBuyStorageForm";
    }

    /**
     * 采购药品入库换厂商换药自动补全
     * @param q
     * @param drugCode
     * @param limit
     * @return
     * @author CTQ
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<BaseDto> autoComplete(String q, String drugCode, String limit) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return drugBuyPlanService.findListByParam(q,drugCode,limit);
    }
    /**
     * 保存修改的采购入库药品信息
     * @param drugBuyPlan
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveDrugBuyPlan")
    @ResponseBody
    public StringData saveDrugBuyPlan(DrugBuyPlan drugBuyPlan) {
        StringData data = new StringData();
        int i = drugBuyPlanService.save(drugBuyPlan);
        if(i > 0){
            data.setCode("success");
            data.setData("修改入库数据成功!");
        }else{
            data.setCode("error");
            data.setData("修改入库数据失败!");
        }
        return data;
    }

    /**
     * 采购药品入库
     * @param drugBuyPlan
     * @author DT
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "buyPlanImportBatch")
    @ResponseBody
    public StringData saveImportBuyPlan(DrugBuyPlan drugBuyPlan) {
        StringData data = new StringData();
        int i = drugBuyPlanService.saveImportBuyPlan(drugBuyPlan);
        if(i > 0){
            data.setCode("success");
            data.setData("采购入库成功!");
        }else{
            data.setCode("error");
            data.setData("入库失败!");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(DrugBuyPlan drugBuyPlan, Model model) {
        StringData data=new StringData();
        int i=drugBuyPlanService.save(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(DrugBuyPlan drugBuyPlan) {
        StringData data=new StringData();
        int i=drugBuyPlanService.delete(drugBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }

}